/**
 * @file dev_uart.c
 * @author lik
 * @brief uart相关配置函数
 * @version 0.1
 * @date 2022-01-14
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "main.h"

/**
 * @brief uart初始化
 * 
 */

uint8_t show_flag;
uint8_t mode_state;

void uart1_init(void)
{
    UART_InitStructure UART_initStruct;

    PORT_Init(PORTA, PIN2, PORTA_PIN2_UART1_RX, 1); //GPIOA.2配置为UART1输入引脚
    PORT_Init(PORTA, PIN0, PORTA_PIN0_UART1_TX, 0); //GPIOA.0配置为UART1输出引脚

    UART_initStruct.Baudrate = 9600;
    UART_initStruct.DataBits = UART_DATA_8BIT;
    UART_initStruct.Parity = UART_PARITY_NONE;
    UART_initStruct.StopBits = UART_STOP_1BIT;
    UART_initStruct.RXThreshold = 3;
    UART_initStruct.RXThresholdIEn = 1;
    UART_initStruct.TXThreshold = 3;
    UART_initStruct.TXThresholdIEn = 0;
    UART_initStruct.TimeoutTime = 10;
    UART_initStruct.TimeoutIEn = 1;
    UART_Init(UART1, &UART_initStruct);
    UART_Open(UART1);
    
}

#if 1
__asm (".global __use_no_semihosting\n\t");
void _sys_exit(int x)
{
  x = x;
}
/* __use_no_semihosting was requested, but _ttywrch was */
void _ttywrch(int ch)
{
    ch = ch;
}
//struct __FILE
//{
//  int handle;
//};
FILE __stdout;

#if defined ( __GNUC__ ) && !defined (__clang__) 
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif 
PUTCHAR_PROTOTYPE
{
    UART_WriteByte(UART1, ch);

    while (UART_IsTXBusy(UART1))
        ;

    return ch;
}
#endif


/**
 * 获取校验码
 * Get checksum code
 */
uint8_t getCheckSum(const uint8_t *pData, int len) {
	int sum = 0;
	for (int i = 0; i < len; ++i) {
		sum += pData[i];
	}
	//printf("---------- %x ", sum);
	return (uint8_t) sum;
}


void uart1_write_buf(uint8_t *buf, uint8_t len)
{
    for (uint32_t i = 0; i < len; i++)
    {
        UART_WriteByte(UART1, buf[i]);
        while (UART_IsTXBusy(UART1))
            ;
    }
}

volatile uint32_t      UART_rx_buf_number;   //通讯数据1帧实际大小
volatile uint8_t       UART_rx_buf[50];      //RX数据缓存buffer
volatile uint8_t       myuart_RX_data[14];    //通讯数据
volatile uint8_t	     myuart_RX_enable=1;
uint8_t tx_buf[] = {0xFF, 0x55, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00};

/*RX数据更新检查 0:无更新  1：有更新*/
uint8_t	update_Check(void)
{
	uint8_t update_tmp=0;

	for(size_t i=0;i<12;i++)			
	{
		if(myuart_RX_data[i] != UART_rx_buf[i+3])
		{
			myuart_RX_data[i]= UART_rx_buf[i+3];
			update_tmp=1;
		}
	}
	return update_tmp;
}

/*RX数据和校验 1：校验通过 0：校验失败 */
uint8_t myuart_RX_check(void)
{
	if ((UART_rx_buf[0]==0xFF)&&(UART_rx_buf[1]==0x55))
	{
     return 1;
	}
	return 0;
}


/*Rx数据解析函数*/
void myuart_RX_analysis(void)
{
    switch (UART_rx_buf[3])
    {
        case 0x00: //显示图片
        /*
            backlight_off_flag = false;
            if(video_play_over_flag)
            {
                if(rx.buf[3]==10||rx.buf[3]==11||rx.buf[3]==12)//高温预警
                    High_Temp_flag = 1;
                else
                    High_Temp_flag = 0;

                ShowPic(rx.buf[3]);
                if(temp1!=rx.buf[3]||temp2!=rx.buf[1])
                {
                    if(rx.buf[3]==10||rx.buf[3]==11||rx.buf[3]==12)
                        ;
                    else
                        work_state = 1;

                    lcd_backlight_on();
                    temp1 = rx.buf[3];
                    response_rx();
                }
            }*/
            show_flag = 1;
        break;

        case 0x01: //
            ST7789_BLK_ON;
        break;

        case 0x02: //
						ST7789_BLK_OFF;
            //if(start_count<=1)
            //    Backlight_startoff_flag = true;
                                    
            //lcd_backlight_off();
            //backlight_off_flag = true;
            //backlight_off_count_flag = true;
        break;

				
        default:
            break;
    }
		mode_state = UART_rx_buf[5];
		
    tx_buf[3] = UART_rx_buf[3];
    tx_buf[4] = UART_rx_buf[4];
    tx_buf[5] = UART_rx_buf[5];
		tx_buf[6] = UART_rx_buf[6];
		tx_buf[7] = UART_rx_buf[7];
		tx_buf[8] = UART_rx_buf[8];
		tx_buf[9] = getCheckSum(tx_buf,9);
    uart1_write_buf(tx_buf, sizeof(tx_buf));

}


void process_rx(void)
{
    if(show_flag)
    {
			ShowStateALL(UART_rx_buf[5],UART_rx_buf[6],UART_rx_buf[7],UART_rx_buf[8]);
			
      show_flag = 0;
    }
}

void UART1_Handler(void)
{

    uint32_t chr;
#if 1
    if (UART_INTStat(UART1, UART_IT_RX_THR | UART_IT_RX_TOUT))
    {
        while (UART_IsRXFIFOEmpty(UART1) == 0)
        {
            if (UART_ReadByte(UART1, &chr) == 0)
            {
                  UART_rx_buf[UART_rx_buf_number++]=(uint8_t)chr;
            }
        }

        if (UART_INTStat(UART1, UART_IT_RX_TOUT))
        {
            UART_INTClr(UART1, UART_IT_RX_TOUT);
            if (myuart_RX_enable&&myuart_RX_check())
            {
                if (update_Check())
                {
                    myuart_RX_analysis();
                }
            }
            // memset(UART_rx_buf,0,UART_rx_buf_number);
            UART_rx_buf_number=0;
        }
    }
#else
    if (UART_INTStat(UART1, UART_IT_RX_THR | UART_IT_RX_TOUT))
    {
        while (UART_IsRXFIFOEmpty(UART1) == 0)
        {
            if (UART_ReadByte(UART1, &chr) == 0)
            {
                //CirBuf_Write(&CirBuf, (uint8_t *)&chr, 1);
                uart1_write_buf(tx_buf, sizeof(tx_buf));
            }
        }

        if (UART_INTStat(UART1, UART_IT_RX_TOUT))
        {
            UART_INTClr(UART1, UART_IT_RX_TOUT);
        }
    }
#endif
}



